using MediatR;
using Medical.Api.Write.Application.Command.DepartmentManagement.Adopted;
using Medical.Domain.DepartmentInfo;
using Medical.ErrorCode;
using Medical.Interstructrue.Base;
using System.Transactions;

namespace Medical.Api.Write.Application.Handler.DepartmentManagement.Adopted
{
    public class DeleteAdoptedCommandHandler : IRequestHandler<DeleteAdoptedCommands, ApiResult<int>>
    {
        private readonly IBaseRepository<TouseTable> touseRep;
        private readonly ILogger<DeleteAdoptedCommandHandler> logger;

        public DeleteAdoptedCommandHandler(IBaseRepository<TouseTable> touseRep, ILogger<DeleteAdoptedCommandHandler> logger)
        {
            this.touseRep = touseRep;
            this.logger = logger;
        }

        public async Task<ApiResult<int>> Handle(DeleteAdoptedCommands request, CancellationToken cancellationToken)
        {
            try
            {
                //开启事务
                using (var tran=new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
                {
                    try
                    {
                        var adoptedId = touseRep.GetModel(request.adoptedId);
                        adoptedId.IsDeleted = true;
                        touseRep.Update(adoptedId);

                        //提交事务
                        tran.Complete();
                        return new ApiResult<int>
                        {
                            Code = ApiEnums.Success,
                            Data = 1,
                            Msg = "领用申请删除成功!"
                        };
                    }
                    catch (Exception)
                    {

                        throw;
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
